{include file="public/head"}
<div class="main">
    {include file="public/left"}
    <div class="middle">
    {include file="public/header"}
    <div class='userinfo'>
        {include file="public/userleft"}
        <div class="user_add">
            <ul>
                <dl>
                    <dd>{:__('用户名')}：</dd>
                    <dd id="username"></dd>
                </dl>
                <dl>
                    <dd>{:__('邮箱地址')}：</dd>
                    <dd id="email_dd"><span id='email'></span><a href="/index/User/updateinfo" id="updateemail">[{:__('修改')}]</a><a id="sendemail">[{:__('发送')}]</a></dd>
                </dl>
                <dl>
                    <dd>{:__('验证码')}：</dd>
                    <dd><input id="captcha" placeholder="{:__('超过五分钟验证码失效')}" type="text" class="layui-input" autocomplete="off"/></dd>
                </dl>
                <dl>
                    <button id="email_verify" class="layui-btn layui-btn-warm">{:__('立即激活')}</button>
                </dl>
            </ul>
        </div>
    </div>
    {include file="public/foot"}
    </div>
    {include file="public/right"}
</div>
{include file="public/footer"}
<script>
    noLog();
    $(document).queue('user',function(){
        var userData = getLocalStorage('userData');
        var userdata = JSON.parse(userData);
        $("#username").html(userdata['username']);
        $("#email").html(userdata['email']);
        if(userdata['emailauth'] == 1){
            $("#email_verify").html("{:__('已认证激活')}");
            $("#email_verify").removeClass("layui-btn-warm");
            $("#email_verify").addClass("layui-btn-disabled");
            $("#email_verify").prop("disabled",true);
            $("#updateemail").css("display","none");
            $("#sendemail").css("display","none");
            $("#captcha").parent().parent().remove();
        }
    });

    $(document).on('click',"#sendemail",function(){
        var events= 'authen';
        var email = $("#email").text();
        var _this = $(this);
        var sendhtml = _this[0].outerHTML;
        layer.open({
            title:"{:__('邮箱验证')}",
            tpye:"2",
            content:"<div class='_captcha'><iframe class='captcha_img' src='/api/captcha'></iframe><div id='_clear' class='_clear'>{:__('看不清，换一张')}</div><input id='captcha' class='layui-input' type='text'/></div>",
            btn: ['{:__("确定")}'],
            yes:function(index, layero){
                $("#sendemail").remove();
                var captcha = $(layero).find("#captcha").val();
                if(captcha == ''){
                    layer.tips("{:__('验证码不能为空')}",$(layero).find("#captcha"));
                    $(".captcha_img")[0].attributes[1].ownerElement.contentWindow.window.location.reload();
                    return false;
                }
                $(".layui-layer-btn0").css("display","none");
                btntmp = "<button id='no_btn' class='layui-btn layui-btn-disabled'>{:__('确定')}</button>";
                $(".layui-layer-btn").append(btntmp);
                $.ajax({
                    url:'/api/EmailCaptcha/send',
                    type:'post',
                    data:{'event':events,'email':email,'captcha':captcha},
                    success:function(ret){
                        if(ret.code != 401 && ret.code != 1){
                            layer.tips(ret.msg,$(layero).find("#captcha"));
                            $(".captcha_img")[0].attributes[1].ownerElement.contentWindow.window.location.reload();
                            $("#no_btn").remove();
                            $(".layui-layer-btn0").css("display","inline-block");
                            var len = $("#sendemail").length;
                            if(len==0){
                                $("#email_dd").append(sendhtml);
                            }
                        } else {
                            layer.msg(ret.msg,{icon:1},function(){
                                layer.close(index);
                                _this.remove();
                                var t = 60;
                                $("#email_dd").append("<a id='sendemail_time'>【"+t+"s】</a>");
                                var interval = setInterval(function(){
                                    t--;
                                    $("#sendemail_time").remove();
                                    if(t == 0){
                                        $("#email_dd").append(sendhtml);
                                        clearInterval(interval);
                                    } else {
                                        $("#email_dd").append("<a id='sendemail_time'>【"+t+"s】</a>");
                                    }
                                }, 1000);
                            });
                        }
                    },error:function(){
                        layer.msg('{:__("网络异常")}',{icon:2},function(){
                            layer.close(index);
                        });
                    }
                });
            }
        });
    });
    $(document).on('click',"#_clear",function(){
        $(".captcha_img")[0].attributes[1].ownerElement.contentWindow.window.location.reload();
    });
    $("#email_verify").click(function(){
        var email = $("#email").text();
        var captcha = $("#captcha").val();
        if(captcha == ''){
            layer.msg("{:__('验证码不能为空')}",{icon:2});
            return false;
        }
        $.ajax({
            url:"/api/user/emailVerify",
            type:'post',
            data:{'email':email,'captcha':captcha,'token':token},
            success:function(ret){
                if(ret.code !=1 && ret.code != 401){
                    layer.msg(ret.msg,{icon:2});
                } else if(ret.code == 1){
                    layer.msg(ret.msg,{icon:1},function(){
                        var userData = getLocalStorage('userData');
                        var userdata = JSON.parse(userData);
                        userdata['emailauth'] = 1;
                        userDatacopy = JSON.stringify(userdata);
                        setLocalStorage('userData',userDatacopy);
                        window.location.reload();
                    });
                }
            }
        });
    });
</script>